package demo.config;

import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.c3p0.C3p0Plugin;
import com.jfinal.plugin.ehcache.EhCachePlugin;
import com.jfinal.render.ViewType;

import demo.controller.HelloController;
import demo.handler.Handler404;
import demo.model.User;

public class JFinalDemoConfig extends JFinalConfig {
	public void configConstant(Constants me) {
		me.setDevMode(true);
		me.setViewType(ViewType.JSP);// default is freemarker
		me.setError404View("/error.html");
	}

	// 跳转的方法和第三个参数没有关系,只要该方法存在,即可在url中直接添加,可以在url添加参数,但是只能添加一级
	public void configRoute(Routes me) {
		// /hello,/hello/abc:则abc为UrlPara,只可加一级
		//me.add("/hello", HelloController.class);
		
		// /hello:404,其他同上
		//me.add("/hello/world", HelloController.class);
		
		// 第三个参数是返回页面前的路径
		me.add("/hello/world", HelloController.class);
		//me.add("/hello/world", HelloController.class, "aaa");
	}

	public void configPlugin(Plugins me) {
		this.loadPropertyFile("classes\\hibernate.properties");
		// 默认是mysql
		C3p0Plugin cp = new C3p0Plugin(this.getProperty("dataSource.url"),
				this.getProperty("dataSource.username"), this.getProperty("dataSource.password"));
		me.add(cp);
		ActiveRecordPlugin arp = new ActiveRecordPlugin(cp);
		arp.setShowSql(true);
		me.add(arp);
		arp.addMapping("user", User.class);
		// arp.addMapping("article", "article_id", Article.class);// 第二个参数指明id的列名,默认是id
		
		me.add(new EhCachePlugin());
	}

	public void configInterceptor(Interceptors me) {
	}

	public void configHandler(Handlers me) {
		Object o = me.getHandlerList();
		me.add(new Handler404());
	}
}
